package com.fengmoxi.ctv.presenter;

import android.content.Context;
import android.view.ContextThemeWrapper;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.core.content.ContextCompat;
import androidx.leanback.widget.ImageCardView;
import androidx.leanback.widget.Presenter;

import com.fengmoxi.ctv.Const;
import com.fengmoxi.ctv.R;
import com.fengmoxi.ctv.model.AppModel;

public class AppCardPresenter extends Presenter {
    private Context mContext;

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent) {
        mContext = parent.getContext();
        ImageCardView cardView = new ImageCardView(new ContextThemeWrapper(mContext, R.style.NoContentImageCardTheme)) {
            @Override
            public void setSelected(boolean selected) {
                int selected_background = ContextCompat.getColor(mContext, R.color.selected_background);
                int default_background = ContextCompat.getColor(mContext, R.color.default_background);
                setInfoAreaBackgroundColor(selected ? selected_background : default_background);
                super.setSelected(selected);
            }
        };
        cardView.setFocusable(true);
        cardView.setFocusableInTouchMode(true);
        cardView.setInfoAreaBackgroundColor(ContextCompat.getColor(mContext, R.color.default_background));
        return new ViewHolder(cardView);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, Object item) {
        ImageCardView cardView = (ImageCardView) viewHolder.view;
        cardView.setMainImageDimensions(Const.CARD_APP_WIDTH, Const.CARD_APP_HEIGHT);
        AppModel appBean = (AppModel) item;
        cardView.setMainImageScaleType(ImageView.ScaleType.CENTER_INSIDE);
        cardView.getMainImageView().setImageDrawable(appBean.getIcon());
        cardView.getMainImageView().setPadding(Const.CARD_PADDING, Const.CARD_PADDING, Const.CARD_PADDING, Const.CARD_PADDING);
        cardView.setTitleText(appBean.getName());
    }

    @Override
    public void onUnbindViewHolder(ViewHolder viewHolder) {
        ImageCardView cardView = (ImageCardView) viewHolder.view;
        cardView.setBadgeImage(null);
        cardView.setMainImage(null);
    }
}